package gang.com.twoshopping.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.Indicators.PagerIndicator;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import gang.com.twoshopping.R;
import gang.com.twoshopping.adapter.BaseAdapter;
import gang.com.twoshopping.adapter.CategoryAdapter;
import gang.com.twoshopping.adapter.HomeCatgoryAdapter;
import gang.com.twoshopping.bean.Banner;
import gang.com.twoshopping.bean.Category;
import gang.com.twoshopping.bean.HomeCategory;
import gang.com.twoshopping.bean.Wares;
import gang.com.twoshopping.http.OkHttpHelper;
import gang.com.twoshopping.http.SpotsCallBack;
import gang.com.twoshopping.utils.Contants;
import gang.com.twoshopping.weight.DividerItemDecoration;
import okhttp3.Response;

/**
 * Created by xiaogangzai on 2017/6/3.
 * 命名规范，从服务器取数据的方法request+。。。+Data
 * 服务器取回来的数据显示到控件，用Show+...+Data
 */

public class CategoryFragment extends Fragment {
    private RecyclerView recyclerView;
    private OkHttpHelper okHttpHelper = OkHttpHelper.getInstance();
    private CategoryAdapter categoryAdapter;


    private SliderLayout msliderShow;
    private PagerIndicator mIndicator;
    private List<Banner> bannerList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.recylerview_categorylist);
        msliderShow = (SliderLayout) view.findViewById(R.id.slider_category);
        mIndicator = (PagerIndicator) view.findViewById(R.id.category_indicator);
        requestCategoryData();
        requestImages();
        return view;
    }

    private void requestCategoryData() {
        okHttpHelper.get(Contants.ApI.CATEGORY_LIST, new SpotsCallBack<List<Category>>(getActivity()) {
            @Override
            public void onSucess(Response response, List<Category> categoryList) {
                showCategoryData(categoryList);
            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });
    }

    private void showCategoryData(final List<Category> datalist) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                categoryAdapter = new CategoryAdapter(getActivity(), datalist);
                recyclerView.setAdapter(categoryAdapter);
                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
                categoryAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                    @Override
                    public void onClick(View v, int position) {

                    }
                });
            }
        });

    }

    private void requestImages() {
        String url = "http://112.124.22.238:8081/course_api/banner/query?type=1";
        okHttpHelper = OkHttpHelper.getInstance();
        okHttpHelper.get(url, new SpotsCallBack<List<Banner>>(getActivity()) {

            @Override
            public void onSucess(Response response, List<Banner> banners) {
                bannerList = banners;
                initSliderview();
            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });
    }

    private void initSliderview() {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (bannerList != null) {
                    for (Banner banner : bannerList) {
                        TextSliderView textSliderView = new TextSliderView(getActivity());
                        textSliderView
                                .description(banner.getName())
                                .image(banner.getImgUrl());
                        msliderShow.addSlider(textSliderView);
                    }
                }
                //这是系统自带的indicator
                //msliderShow.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
                msliderShow.setPresetTransformer(SliderLayout.Transformer.RotateUp);
                msliderShow.setDuration(3000);
                //使用自定义的indicator
                msliderShow.setCustomIndicator(mIndicator);
                msliderShow.setCustomAnimation(new DescriptionAnimation());
                //添加监听事件
                msliderShow.addOnPageChangeListener(new ViewPagerEx.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {

                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });
            }
        });

    }
}
